<?php
/**
 * @description : show the video view informations.
 * @class-name : ListVideoAccessAction()
 * @author : stephen.mo <stephen.mo@achievo.com>
 * @date : 2010-04-12
 */
require_once 'TVB/Config/requires.php';
require_once 'TVB/ObjectRecord/all.php';
require_once 'TVB/mytvplus/MstatReportTool/common/AppConfiguration.class.php' ;
require_once 'TVB/mytvplus/MstatReportTool/common/ExportData.class.php';

class MstatReportExport extends BaseAction {
    private function exportFileWithHTML($content,$file_name_prefix,$export_style_suffix) {
        $data = ExportData::getHTMLbody($content);
        Header("Last-Modified: " . date("D, d M Y H:i:s") . " GMT");
        Header("Content-type: application/html;\n");
        Header("Content-Transfer-Encoding: binary");
        $len = strlen($data);
        Header("Content-Length: $len;\n");
        $output_file_name = $file_name_prefix.'_'.date('Y-m-d').$export_style_suffix;
        Header("Content-Disposition: attachment; filename=\"$output_file_name\";\n\n");
        echo $data;
        exit;
    }

    private function exportFileWithCSV($content,$file_name_prefix,$export_style_suffix) {
        $content = str_replace('<a href="#" class="print">Print</a>','',$content);
        
        //only get a header.
        $data = ExportData::getHTMLbody($content,false);
        Header("Last-Modified: " . date("D, d M Y H:i:s") . " GMT");
        Header("Content-type: application/html;\n");
        Header("Content-Transfer-Encoding: binary");
        $len = strlen($data);
        Header("Content-Length: $len;\n");
        $output_file_name = $file_name_prefix.'_'.date('Y-m-d').'.xls';
        Header("Content-Disposition: attachment; filename=\"$output_file_name\";\n\n");
        echo $data;
        exit;
    }

    private function exportFileWithPDF($content) {
        return 'developing...!';
    }

    public function execute() {
        $rand_code = $_GET['randcode'];            
        $file_name_prefix = $_GET['type'];
        $export_style = $_GET['style'];
        $export_style_suffix = '.'.strtolower($export_style);

        $base_path = AC::DEFAULT_BASE_LIB_PATH;
        $file_path = AC::EXPORT_TMP_FILE_PATH;
        $file_name = AC::EXPORT_TMP_FILE_NAME.$rand_code.'.tmp';
        $real_file_path = $base_path.'/'.$file_path;
        $real_file_name = $real_file_path.'/'.$file_name;
        if (is_file($real_file_name)) {
            //read the tmp body.
            $handle = fopen($real_file_name,'r');
            $content = fread($handle,filesize($real_file_name));
            fclose($handle);

            //$data = ExportData::getHTMLbody($content);
            $export_method = 'exportFileWith'.strtoupper($export_style);
            $data = $this->{$export_method}($content,$file_name_prefix,$export_style_suffix);
            exit;
        }

        return 'success';
    }    
}

?>